﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static NotesInfo;

public class PerfectDetect : MonoBehaviour
{
    private int layer;

    public static Queue<GameObject> perfectNotesLeft = new Queue<GameObject>();
    public static Queue<GameObject> perfectNotesRight = new Queue<GameObject>();

    public static GameObject holdLeft;
    public static GameObject holdRight;

    private void Start()
    {
        perfectNotesLeft.Clear();
        perfectNotesRight.Clear();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Note") && layer == collision.gameObject.GetComponent<NotesGenerate>().layer)
        {
            if (GetNoteType(collision.gameObject) != 3)
            {
                if (GetNoteDirection(collision.gameObject))
                {
                    perfectNotesLeft.Enqueue(collision.gameObject);
                }
                else
                {
                    perfectNotesRight.Enqueue(collision.gameObject);
                }
            }
            else
            {
                if (GetNoteDirection(collision.gameObject))
                {
                    holdLeft = collision.gameObject;
                }
                else
                {
                    holdRight = collision.gameObject;
                }
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Note") && layer == collision.gameObject.GetComponent<NotesGenerate>().layer)
        {
            if (GetNoteType(collision.gameObject) != 3)
            {
                if (perfectNotesLeft.Count != 0)
                {
                    if (perfectNotesLeft.Peek() == collision.gameObject)
                    {
                        perfectNotesLeft.Dequeue();
                    }

                }
                if (perfectNotesRight.Count != 0)
                {
                    if (perfectNotesRight.Peek() == collision.gameObject)
                    {
                        perfectNotesRight.Dequeue();
                    }
                }
            }
            else
            {
                if (GetNoteDirection(collision.gameObject))
                {
                    holdLeft = null;
                }
                else
                {
                    holdRight = null;
                }
            }

            if (collision.gameObject.GetComponent<NotesGenerate>().doEnabled)
            {
                var tmpNoteType = GetNoteType(collision.gameObject);
                if (tmpNoteType == 1 || tmpNoteType == 2)
                {
                    ScoreManager.Miss();
                    collision.gameObject.GetComponent<NotesGenerate>().ClearGameObject(3);
                }
                else if (tmpNoteType == 3)
                {
                    ScoreManager.Perfect();
                    collision.gameObject.GetComponent<NotesGenerate>().ClearGameObject(0);
                }
            }

        }
    }
}
